
using System.Web.UI.HtmlControls;
using System;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Web.UI.WebControls;
using Microsoft.VisualBasic;
using System.Collections;
using System.Web.UI;
using System.Web;


using System.Text;
using com.ddsc.common;
//using com.ddsc.database;
using DDSC.OracleADO;


namespace smasb2b
{
	public class EMPD0011 : BasePage
	{
		
#region  Web Form 設計工具產生的程式碼
		
		//此為 Web Form 設計工具所需的呼叫。
		[System.Diagnostics.DebuggerStepThrough()]private void InitializeComponent()
		{
			
		}
		protected ddscControlLibrary.InputBoxCtrl txtPROD1;
		protected ddscControlLibrary.InputBoxCtrl txtPROD2;
		protected ddscControlLibrary.InputBoxCtrl txtPROD3;
		protected ddscControlLibrary.InputBoxCtrl txtPROD4;
		protected System.Web.UI.HtmlControls.HtmlImage IMG1;
		protected System.Web.UI.HtmlControls.HtmlTable DataGrid;
		protected ddscControlLibrary.SubmitButtonCtrl btnSubmit;
		protected ddscControlLibrary.InputBoxCtrl txtVND_EMAIL;
		protected ddscControlLibrary.InputBoxCtrl txtCOMPANY_NAME;
		protected ddscControlLibrary.InputBoxCtrl txtCOMPANY_DEPT;
		protected ddscControlLibrary.InputBoxCtrl txtCUST_TEL;
		protected ddscControlLibrary.InputBoxCtrl txtCUST_EMAIL;
		protected ddscControlLibrary.InputBoxCtrl txtCUST_NAME;
		protected ddscControlLibrary.InputBoxCtrl txtEMP_ID;
		protected System.Web.UI.WebControls.HyperLink hlOrderLink;
		protected System.Web.UI.WebControls.Panel pnlResult;
		protected System.Web.UI.WebControls.PlaceHolder phOrder;
		protected ddscControlLibrary.InputBoxCtrl Inputboxctrl1;
		protected System.Web.UI.WebControls.RadioButtonList cblPayType;
		
		//注意: 下列預留位置宣告是 Web Form 設計工具需要的項目。
		//請勿刪除或移動它。
		private System.Object designerPlaceholderDeclaration;
		
		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: 此為 Web Form 設計工具所需的方法呼叫
			//請勿使用程式碼編輯器進行修改。
			InitializeComponent();
		}
		
#endregion
		
		private void Page_Load(System.Object sender, System.EventArgs e)
		{
			//在這裡放置使用者程式碼以初始化網頁
		}
		
		private void btnSUBMIT_Click(System.Object sender, System.EventArgs e)
		{
			dbAccess dba = new dbAccess();
			com.ddsc.common.ddscDataSet ddscDs = new com.ddsc.common.ddscDataSet();
			com.ddsc.common.ddscDataTable ddscDtM = ddscDs.createDataTable("EMP_ORDER_MASTER");
			com.ddsc.common.ddscDataTable ddscDtD = ddscDs.createDataTable("EMP_ORDER_DETAILS");
			
			try
			{
				string strOrderNo = getOrderNo();
				
				ddscDtM.addNewRow();
				ddscDtM.setColumnValue("action", "string", "add");
				ddscDtM.setColumnValue("ORDER_NO", "string", strOrderNo);
				ddscDtM.setColumnValue("ORDER_DATE", "string", SystemDate);
				ddscDtM.setColumnValue("EMP_ID", "string", txtEMP_ID.Text.Trim());
				ddscDtM.setColumnValue("COMPANY_NAME", "string", txtCOMPANY_NAME.Text.Trim());
				ddscDtM.setColumnValue("COMPANY_DEPT", "string", txtCOMPANY_DEPT.Text.Trim());
				ddscDtM.setColumnValue("CUST_TEL", "string", txtCUST_TEL.Text.Trim());
				ddscDtM.setColumnValue("CUST_NAME", "string", txtCUST_NAME.Text.Trim());
				ddscDtM.setColumnValue("CUST_EMAIL", "string", txtCUST_EMAIL.Text.Trim());
				ddscDtM.setColumnValue("PAY_TYPE", "string", cblPayType.SelectedValue);
				ddscDtM.setColumnValue("STATUS", "string", "1");
				ddscDtM.setColumnValue("CRT_DATE", "string", SystemDate);
				ddscDtM.setColumnValue("CRT_TIME", "string", SystemTime);
				ddscDtM.setColumnValue("UPD_DATE", "string", SystemDate);
				ddscDtM.setColumnValue("UPD_TIME", "string", SystemTime);
				
				int n = 1;
				string strProdId = "1";
				string strProdName = "";
				string strProdSpec = "";
				string strOrderQty = txtPROD1.Text.Trim();
				int nUnitPrice = 300;
				
				strProdId = "1";
				strProdName = "機上嚴選-銘果物語(輕巧包)";
				strProdSpec = "100 包 / 箱，14G ± 5% / 包";
				strOrderQty = txtPROD1.Text.Trim();
				nUnitPrice = 300;
				if (strOrderQty != string.Empty && Information.IsNumeric(strOrderQty))
				{
					int nOrderAmt = int.Parse(strOrderQty) * nUnitPrice;
					
					if (nOrderAmt != 0)
					{
						ddscDtD.addNewRow();
						ddscDtD.setColumnValue("action", "string", "add");
						ddscDtD.setColumnValue("ORDER_NO", "string", strOrderNo);
						ddscDtD.setColumnValue("ORDER_SEQ", "string", n.ToString());
						ddscDtD.setColumnValue("PROD_ID", "string", strProdId);
						ddscDtD.setColumnValue("PROD_NAME", "string", strProdName);
						ddscDtD.setColumnValue("PROD_SPEC", "string", strProdSpec);
						ddscDtD.setColumnValue("UNIT_PRICE", "string", nUnitPrice.ToString());
						ddscDtD.setColumnValue("ORDER_QTY", "string", strOrderQty);
						ddscDtD.setColumnValue("ORDER_AMT", "string", nOrderAmt);
						ddscDtD.setColumnValue("STATUS", "string", "1");
						ddscDtD.setColumnValue("CRT_DATE", "string", SystemDate);
						ddscDtD.setColumnValue("CRT_TIME", "string", SystemTime);
						ddscDtD.setColumnValue("UPD_DATE", "string", SystemDate);
						ddscDtD.setColumnValue("UPD_TIME", "string", SystemTime);
						
						n++;
					}
				}
				
				strProdId = "2";
				strProdName = "機上嚴選 - 果舖物語米果";
				strProdSpec = "60 包 / 箱，20G ± 5% / 包";
				strOrderQty = txtPROD2.Text.Trim();
				nUnitPrice = 570;
				if (strOrderQty != string.Empty && Information.IsNumeric(strOrderQty))
				{
					int nOrderAmt = int.Parse(strOrderQty) * nUnitPrice;
					
					if (nOrderAmt != 0)
					{
						ddscDtD.addNewRow();
						ddscDtD.setColumnValue("action", "string", "add");
						ddscDtD.setColumnValue("ORDER_NO", "string", strOrderNo);
						ddscDtD.setColumnValue("ORDER_SEQ", "string", n.ToString());
						ddscDtD.setColumnValue("PROD_ID", "string", strProdId);
						ddscDtD.setColumnValue("PROD_NAME", "string", strProdName);
						ddscDtD.setColumnValue("PROD_SPEC", "string", strProdSpec);
						ddscDtD.setColumnValue("UNIT_PRICE", "string", nUnitPrice.ToString());
						ddscDtD.setColumnValue("ORDER_QTY", "string", strOrderQty);
						ddscDtD.setColumnValue("ORDER_AMT", "string", nOrderAmt);
						ddscDtD.setColumnValue("STATUS", "string", "1");
						ddscDtD.setColumnValue("CRT_DATE", "string", SystemDate);
						ddscDtD.setColumnValue("CRT_TIME", "string", SystemTime);
						ddscDtD.setColumnValue("UPD_DATE", "string", SystemDate);
						ddscDtD.setColumnValue("UPD_TIME", "string", SystemTime);
						
						n++;
					}
				}
				
				strProdId = "3";
				strProdName = "長榮巧飲 - 橙果汁";
				strProdSpec = "150ML × 24 入 / 箱";
				strOrderQty = txtPROD3.Text.Trim();
				nUnitPrice = 125;
				if (strOrderQty != string.Empty && Information.IsNumeric(strOrderQty))
				{
					int nOrderAmt = int.Parse(strOrderQty) * nUnitPrice;
					
					if (nOrderAmt != 0)
					{
						ddscDtD.addNewRow();
						ddscDtD.setColumnValue("action", "string", "add");
						ddscDtD.setColumnValue("ORDER_NO", "string", strOrderNo);
						ddscDtD.setColumnValue("ORDER_SEQ", "string", n.ToString());
						ddscDtD.setColumnValue("PROD_ID", "string", strProdId);
						ddscDtD.setColumnValue("PROD_NAME", "string", strProdName);
						ddscDtD.setColumnValue("PROD_SPEC", "string", strProdSpec);
						ddscDtD.setColumnValue("UNIT_PRICE", "string", nUnitPrice.ToString());
						ddscDtD.setColumnValue("ORDER_QTY", "string", strOrderQty);
						ddscDtD.setColumnValue("ORDER_AMT", "string", nOrderAmt);
						ddscDtD.setColumnValue("STATUS", "string", "1");
						ddscDtD.setColumnValue("CRT_DATE", "string", SystemDate);
						ddscDtD.setColumnValue("CRT_TIME", "string", SystemTime);
						ddscDtD.setColumnValue("UPD_DATE", "string", SystemDate);
						ddscDtD.setColumnValue("UPD_TIME", "string", SystemTime);
						
						n++;
					}
				}
				
				strProdId = "4";
				strProdName = "長榮巧飲 - 蘋果汁";
				strProdSpec = "150ML × 24 入 / 箱";
				strOrderQty = txtPROD4.Text.Trim();
				nUnitPrice = 125;
				if (strOrderQty != string.Empty && Information.IsNumeric(strOrderQty))
				{
					int nOrderAmt = int.Parse(strOrderQty) * nUnitPrice;
					
					if (nOrderAmt != 0)
					{
						ddscDtD.addNewRow();
						ddscDtD.setColumnValue("action", "string", "add");
						ddscDtD.setColumnValue("ORDER_NO", "string", strOrderNo);
						ddscDtD.setColumnValue("ORDER_SEQ", "string", n.ToString());
						ddscDtD.setColumnValue("PROD_ID", "string", strProdId);
						ddscDtD.setColumnValue("PROD_NAME", "string", strProdName);
						ddscDtD.setColumnValue("PROD_SPEC", "string", strProdSpec);
						ddscDtD.setColumnValue("UNIT_PRICE", "string", nUnitPrice.ToString());
						ddscDtD.setColumnValue("ORDER_QTY", "string", strOrderQty);
						ddscDtD.setColumnValue("ORDER_AMT", "string", nOrderAmt);
						ddscDtD.setColumnValue("STATUS", "string", "1");
						ddscDtD.setColumnValue("CRT_DATE", "string", SystemDate);
						ddscDtD.setColumnValue("CRT_TIME", "string", SystemTime);
						ddscDtD.setColumnValue("UPD_DATE", "string", SystemDate);
						ddscDtD.setColumnValue("UPD_TIME", "string", SystemTime);
						
						n++;
					}
				}
				
				ddscDs.addDataTable(ddscDtM);
				ddscDs.addDataTable(ddscDtD);
				SQLConverter converter = new SQLConverter();
				string[] arrSql = converter.convertToSQL(ddscDs.getDataSet());
				DataSet ds = dba.runExecuteNonQuery(arrSql);
				
				if (ds.Tables[0].Rows[0]["TransResult"].ToString() == "T")
				{
					FunctionHandler fn = new FunctionHandler();
					string strEncrypted = fn.Encrypt(strOrderNo);
					phOrder.Visible = false;
					pnlResult.Visible = true;
					hlOrderLink.NavigateUrl = "EMPD0012.aspx?o=" + strEncrypted;
					
					this.RegisterStartupScript("Result", "<script language=\"javascript\">alert(\'訂單資料已經送出成功！\\n\\n訂單編號：" + strOrderNo + "\');window.open(\'EMPD0012.aspx?o=" + Server.UrlEncode(strEncrypted) + "\');</script>");
					clearText();
				}
				else
				{
					this.RegisterStartupScript("Result", "<script language=\"javascript\">alert(\'" + ds.Tables[0].Rows[0]["Message"].ToString().Replace("\'", string.Empty).Replace(System.Environment.NewLine, "\\n") + "\');</script>");
				}
				
			}
			catch (Exception ex)
			{
				showException(ex.Message);
			}
		}
		
		private string getOrderNo()
		{
			dbAccess dba = new dbAccess();
			StringBuilder sb = new StringBuilder();
            ArrayList m_arrParam = new ArrayList();
			sb.AppendFormat(" SELECT MAX (SUBSTR(order_no, 1, 12)) ");
			sb.AppendFormat("   FROM emp_order_master ");
			//sb.AppendFormat("  WHERE order_date = \'{0}\' ", SystemDate);
            sb.AppendFormat("  WHERE order_date = :SystemDate");
            //Ken : 2.設定動態參數物件
            m_arrParam.Add(dba.GetOracleParam(":SystemDate", SystemDate));
            //Ken : 3.修改執行方法Overload
            //DataSet ds = dba.runExecuteQuery("DATA", sb.ToString());
            DataSet ds = dba.runExecuteQuery("DATA", sb.ToString(), m_arrParam);

			string strOrderNo = DateTime.Now.ToString("yyyyMMdd") + "0001";
			
			if (ds.Tables[0].Rows[0]["ReturnCode"].ToString() == "000")
			{
				string strMaxOrderNo = System.Convert.ToString(ds.Tables["DATA"].Rows[0][0].ToString());
				if (strMaxOrderNo != string.Empty)
				{
					int nSeq = int.Parse(strMaxOrderNo.Substring(9));
					
					nSeq++;
					strOrderNo = System.Convert.ToString(strMaxOrderNo.Substring(0, 8) + string.Format("{0:0000}", nSeq));
				}
			}
			
			return strOrderNo + getCheckNumber(strOrderNo);
		}
		
		private dynamic getCheckNumber(string strOrderNo)
		{
			int sum = 0;
			
			for (int i = 0; i <= strOrderNo.Length - 1; i++)
			{
				int n = int.Parse(strOrderNo[i].ToString());
				
				sum += n;
			}
			
			return (sum % 7).ToString();
		}
		
		private void clearText()
		{
			txtEMP_ID.Text = string.Empty;
			txtCOMPANY_NAME.Text = string.Empty;
			txtCOMPANY_DEPT.Text = string.Empty;
			txtCUST_EMAIL.Text = string.Empty;
			txtCUST_TEL.Text = string.Empty;
			txtCUST_NAME.Text = string.Empty;
			txtPROD1.Text = "0";
			txtPROD2.Text = "0";
			txtPROD3.Text = "0";
			txtPROD4.Text = "0";
		}
	}
	
}
